package com.xiaoshu.disruptor.handler;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lmax.disruptor.EventHandler;
import com.xiaoshu.aop.LevelRecordAnnotation;
import com.xiaoshu.disruptor.MessageEvent;
import com.xiaoshu.user.entity.ScoreLog;
import com.xiaoshu.user.service.IScoreLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 功能说明： 将用户的积分数据保存到数据库中；
 * com.xiaoshu.disruptor.handler
 *
 * <p>
 * Original @Author: deane.jia-贾亮亮,2021/6/15 14:15
 * <p>
 * Copyright (C)2012-2021 小树盛凯科技 All rights reserved.
 */
@Component
@Slf4j
public class SaveScoreEventHandler implements EventHandler<MessageEvent> {

    private IScoreLogService scoreLogService;

    @Autowired
    public void setScoreLogService(IScoreLogService scoreLogService) {
        this.scoreLogService = scoreLogService;
    }

    @Override
    public void onEvent(MessageEvent event, long sequence, boolean endOfBatch) throws Exception {
        log.info("====> [Save Score] info:{}", event.getMethodInfoModel().toString());
        // TODO  step-1 保存用户的分数信息到数据库
        Method method = event.getMethodInfoModel().getMethod();
        Object[] params = event.getMethodInfoModel().getParams();
        Map<String, Object> annotationValueMap = AnnotationUtil.getAnnotationValueMap(method, LevelRecordAnnotation.class);
        if(ObjectUtil.isNotEmpty(annotationValueMap)){
            String value = (String)annotationValueMap.get("value");
            log.info("=_=#> [Score Type] value :{}", value );
            scoreLogService.save( new ScoreLog((String) params[0], value, 10.0F));
        }
    }
}
